package com.itheima.service;

import com.github.pagehelper.PageHelper;
import com.itheima.domain.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class StudentServiceTest {
  //查询全部
@Test
    public void findAll(){
    //1.获取SqlSession对象
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    //2.获取mapper的代理对象
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    //3.调用代理对象方法
    List<Student> list = mapper.findAll();
    for (Student student : list) {
        System.out.println(student);
    }
    sqlSession.close();
}
    @Test
    public void selectById(){
        //1.获取SqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //2.获取mapper的代理对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //3.调用代理对象方法
        Student id = mapper.selectById(1);
        System.out.println(id);
        sqlSession.close();
    }
    @Test
    public void selectInto(){
        //1.获取SqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //2.获取mapper的代理对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //3.调用代理对象方法
        Student stu =new Student(8,"章若楠",23);
        Integer integer = mapper.selectInto(stu);
        System.out.println(integer);
        sqlSession.close();
    }
    @Test
    public void update(){
        //1.获取SqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //2.获取mapper的代理对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //3.调用代理对象方法
        Student stu =new Student(10,"貂蝉",23);
        Integer integer = mapper.update(stu);
        System.out.println(integer);
        sqlSession.close();
    }
    @Test
    public void delete(){
        //1.获取SqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //2.获取mapper的代理对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //3.调用代理对象方法

        Integer integer = mapper.delete(8);
        System.out.println(integer);
        sqlSession.close();
    }



}
